<template>
    <div v-loading="true" style="height:100vh;background: #999">
    </div>
</template>

<script>
    import { getToken, setToken, removeToken } from '@/utils/auth'
    import NProgress from 'nprogress'
    import 'nprogress/nprogress.css'
    NProgress.configure({ showSpinner: true })
    import axios from 'axios'
    export default {
        name: "redirect",
      data() {
        return {
          url:""
        }
        },
        created(){
          this.url="http://grow.xxzzu.com/prod-api";
          NProgress.start();
          this.url=1
          this.systemInfo();
        },
        methods:{
          systemInfo(){
            let _this=this;
            axios.get(`http://grow.xxzzu.com/prod-api/weixin/system-info`,{}).then(function(r){
              if (r.data.code == 200) {
                _this.getUserInfo(r.data.data);
              } else {
                location.href = '/login';
              }
            }).catch(function(error){
              location.href = '/login';
              console.log(error)//错误处理 相当于error
            })
          },
          getUserInfo(d){
            let _this=this;
            const code = this.$route.query.code;
            const state = this.$route.query.state;
            let data={
              "authorizeCode": code,
              "state": state,
              "appId": d.appId,
              "bizId": d.bizId}
            axios.post(`http://grow.xxzzu.com/prod-api/wechat/user-info`, data,{

            }).then(function(r){
              if (r.data.code == 200) {
                _this.login(r.data.data);
              } else {
                location.href = '/login';
              }
            }).catch(function(error){
              location.href = '/login';
              console.log(error)//错误处理 相当于error
            })
          },
          login(userInfo)
          {
            const code = this.$route.query.code;
            const state = this.$route.query.state;
            axios.post(`http://grow.xxzzu.com/prod-api/weixin/login`,
              JSON.stringify(userInfo)
            ).then(function(r){
              if (r.data.code == 200) {
                setToken(r.data.token)
                location.href = '/index';
              } else {
                location.href = '/login';
              }
            }).catch(function(error){
              location.href = '/login';
              console.log(error)//错误处理 相当于error
            })
          }
        }
    }
</script>

<style scoped>

</style>
